package com.example.recycleview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

import com.example.recycleview.adapter.FruitAdapter;
import com.example.recycleview.entity.Fruit;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private List<Fruit> fruitList = new ArrayList<>();
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFruits(); // 初始化数据
        recyclerView = findViewById(R.id.recycle_view);

        // 1/3用于指定recycleView的布局方式，这里采用的是垂直线性布局
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // 2/3设置水平线性布局
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
//        recyclerView.setLayoutManager(layoutManager);

        // 3/3瀑布布局 --设置水平方向每行排列三个元素
       // StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
       // recyclerView.setLayoutManager(layoutManager);

        // 将Adapter设置到RecycleView中
        recyclerView.setAdapter(new FruitAdapter(fruitList));

        // 添加滑动监听
        addRecyclerViewScrollExpose();
    }

    int i = 0;
    private void addRecyclerViewScrollExpose() {
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                    Log.i(TAG, "onScrollStateChanged: dragging  " + i++);
                }
            }
        });
    }


    private void initFruits() {
        Fruit apple = new Fruit(getRandomLengthName("Apple"), R.drawable.apple);
        Fruit origin = new Fruit(getRandomLengthName("Origin"), R.drawable.origin);
        Fruit banana = new Fruit(getRandomLengthName("Banana"), R.drawable.banana);
        for (int i = 0; i < 10; i++) {
            fruitList.add(apple);
            fruitList.add(origin);
            fruitList.add(banana);
        }
    }

    //随机生成不同长度的字符串,用于实现瀑布布局的效果展示
    private String getRandomLengthName(String name) {
        Random random = new Random();
        int length = random.nextInt(20 + 1);
        Log.i(TAG, "getRandomLengthName: length" + length);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            stringBuilder.append(name + " ");
        }
        return stringBuilder.toString();
    }
}